package database.good;

import com.sun.istack.internal.NotNull;
import database.CRUD;

import javax.swing.table.DefaultTableModel;
import java.sql.*;

public class tb_good {//商品表
    @NotNull
    private String Gid;//商品编号
    @NotNull
    private String Gname;//商品名字
    @NotNull
    private float Gpay;//商品价格
    @NotNull
    private String Cid;//客户编号
    private String GIntroduction;//商品简介
    private String other;//备注
    public tb_good(String Gid, String Gname, float Gpay, String Cid, String GIntroduction, String other){
        this.Gid = Gid;
        this.Gname = Gname;
        this.Gpay = Gpay;
        this.Cid = Cid;
        this.GIntroduction = GIntroduction;
        this.other = other;
    };
    public tb_good(){};

    public void setCid(String cid) {
        Cid = cid;//以字符串“gys”为编号前缀，加上4位数字作编号的后缀，这4位数字从1000开始。例如（gys1001）
    }
    public void setGid(String gid) {
        Gid = gid;//以字符串“sp”为编号的前缀，加上4位数字作编号的后缀，这8位数字从00000001开始。例如（sp00000001）
    }
    public void setGname(String gname) {
        Gname = gname;
    }
    public void setGpay(float gpay) {
        Gpay = gpay;
    }
    public void setGIntroduction(String gIntroduction) {
        GIntroduction = gIntroduction;
    }
    public void setOther(String other) {
        this.other = other;
    }
    public String getGid() {
        return Gid;
    }
    public String getGname() {
        return Gname;
    }
    public float getGpay() {
        return Gpay;
    }
    public String getGIntroduction() {
        return GIntroduction;
    }
    public String getOther() {
        return other;
    }


    // 插入新商品
    public boolean insertGood(String Gid, String Gname, float Gpay, String Cid, String GIntroduction, String other) {
        String sql = "INSERT INTO tb_good (Gid, Gname, Gpay, Cid, GIntroduction, other) "
                + "VALUES (?, ?, ?, ?, ?, ?)";
        try (Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=UTC", "root", "123456");
             PreparedStatement pstmt = conn.prepareStatement(sql)) {

            // 设置预编译语句的参数值
            pstmt.setString(1, Gid);
            pstmt.setString(2, Gname);
            pstmt.setFloat(3, Gpay);
            pstmt.setString(4, Cid);
            pstmt.setString(5, GIntroduction);
            pstmt.setString(6, other);

            // 执行更新操作
            int rowsAffected = pstmt.executeUpdate();

            if (rowsAffected > 0) {
                return true; // 插入成功
            } else {
                return false; // 插入失败
            }

        } catch (SQLException e) {
            e.printStackTrace();
            return false; // 插入失败
        }
    }


    // 更新商品信息
    public boolean updateGood(String Gid, String Gname, float Gpay, String Cid, String GIntroduction, String other) {
        String sql = "UPDATE tb_good SET Gname = ?, Gpay = ?, Cid = ?, GIntroduction = ?, other = ? "
                + "WHERE Gid = ?";
        Object[] params = {Gname, Gpay, Cid, GIntroduction, other, Gid};
        try {
            CRUD.executeUpdate(sql);
            return true; // 更新成功
        } catch (SQLException e) {
            e.printStackTrace();
            return false; // 更新失败
        }
    }

    // 删除商品
    public boolean deleteGood(String attribute, String value) {//需要调整为检测输入
        String sql;
        if ("Gid".equalsIgnoreCase(attribute)) {
            //如果是Eid的的话，直接查询
            sql = "DELETE FROM tb_good WHERE Gid = ?";
        } else {
            // 如果是其他的需要添加属性查询
            sql = "SELECT * FROM tb_good WHERE " + attribute + " = ?";
        }

        try {
            PreparedStatement pstmt = CRUD.getConnection().prepareStatement(sql);
            pstmt.setString(1, value);

            if ("Eid".equalsIgnoreCase(attribute)) {
                int rowsAffected = pstmt.executeUpdate();
                return rowsAffected > 0;
            } else {
                //sql查询出来的行
                ResultSet rs = pstmt.executeQuery();
                int rowCount = 0;
                while (rs.next()) {
                    rowCount++;
                    String gid = rs.getString("Gid");
                    String gname = rs.getString("Gname");
                    System.out.println("找到商品：Gid=" + gid + ", Gname=" + gname);
                }

                if (rowCount == 0) {
                    System.out.println("没有找到 " + attribute + " = " + value + " 的商品");
                    return false;
                } else if (rowCount == 1) {

                    int rowsAffected = pstmt.executeUpdate();
                    return rowsAffected > 0;
                } else {
                    System.out.println("找到多个符合条件的商品 " + attribute + " = " + value);
                    return false;
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return false; // 删除失败
        }
    }

    // 查询商品信息
    public DefaultTableModel queryGood() {
        String sql = "SELECT * FROM tb_good ";
        try {
            return CRUD.executeQueryAndBuildTableModel(sql);
        } catch (SQLException e) {
            e.printStackTrace();
            return null; // 查询失败
        }
    }
}
